Lang¶
The base language library (import (lang)) provides the most bare-bones
standard scheme syntax and functions to get started.
Warning
This library is largely incomplete and not everything that scheme-rs has
to offer! Try out (import (rnrs)) for a more complete language and consult
the r6rs specification and
r6rs standard libraries specification.
define syntax¶
Defining variables¶
Defines a variable with the name variable and binds it to the result of expression
| Example: | |
|---|---|
Defining funtions¶
Defines a function. A function can have any number of arguments and optionally end
in . variable-name to express variable arity.
lambda syntax¶
The lambda keyword is used to define anonymous procedures. It has three key forms:
(lambda (⟨arg1⟩ ... ⟨argn⟩) ⟨body⟩)Defines an anonymous procedure that takesnarguments and applies them to body.(lambda (⟨arg1⟩ ... ⟨argn⟩ . ⟨var args⟩) ⟨body⟩)Defines an anonymous procedure that takes at leastnarguments and applies them to body. Any extra arguments are bound to⟨var args⟩as a list.(lambda ⟨args⟩ ⟨body⟩)Defines an anonymous procedure that takes any number of arguments and binds them to⟨args⟩.
lambda functions can capture their environment. That is to say, variables
bound outside the scope of the lambda are captured.
| Example: captured variables | |
|---|---|
let, let* and letrec syntax¶
let¶
The let keyword is used to define lexical bindings for local variables.
Variables defined this way are only visible for their body. Variables are bound to their expressions in order, but are not visible for each others binding expressions.
Let expressions return the last value returned in body.
let*¶
let* is similar to let, but each subsequent binding has access to the
previous:
The following code
is equivalent to:
letrec¶
letrec allows for the creation of recursive (or even mutually recursive) bindings:
| Example: letrec factorial | |
|---|---|
set! syntax¶
set! allows for the mutation of variables.
Values that are exported are considered to be immutable and attempting to set them is a syntax violation.
quote syntax¶
quote returns its argument literally without evaluation. It is often referred
to by it's alias, '.
include syntax¶
Inserts the contents of ⟨filename⟩ at the point of expansion.
Pattern matching¶
match syntax¶
Provides facilities for pattern matching. The pattern matcher provided by
scheme-rs is known as the Wright-Cartwright-Shinn matcher thanks to its
authors. It has the following grammar:
patterns: matches:
pat ::= identifier anything, and binds identifier
| _ anything
| () the empty list
| #t #t
| #f #f
| string a string
| number a number
| character a character
| 'sexp an s-expression
| 'symbol a symbol (special case of s-expr)
| (pat_1 ... pat_n) list of n elements
| (pat_1 ... pat_n . pat_{n+1}) list of n or more
| (pat_1 ... pat_n pat_n+1 ooo) list of n or more, each element
of remainder must match pat_n+1
| #(pat_1 ... pat_n) vector of n elements
| #(pat_1 ... pat_n pat_n+1 ooo) vector of n or more, each element
of remainder must match pat_n+1
| #&pat box
| ($ record-name pat_1 ... pat_n) a record
| (= field pat) a ``field'' of an object
| (and pat_1 ... pat_n) if all of pat_1 thru pat_n match
| (or pat_1 ... pat_n) if any of pat_1 thru pat_n match
| (not pat_1 ... pat_n) if all pat_1 thru pat_n don't match
| (? predicate pat_1 ... pat_n) if predicate true and all of
pat_1 thru pat_n match
| (set! identifier) anything, and binds setter
| (get! identifier) anything, and binds getter
| `qp a quasi-pattern
| (identifier *** pat) matches pat in a tree and binds
identifier to the path leading
to the object that matches pat
ooo ::= ... zero or more
| ___ zero or more
| **1 1 or more
quasi-patterns: matches:
qp ::= () the empty list
| #t #t
| #f #f
| string a string
| number a number
| character a character
| identifier a symbol
| (qp_1 ... qp_n) list of n elements
| (qp_1 ... qp_n . qp_{n+1}) list of n or more
| (qp_1 ... qp_n qp_n+1 ooo) list of n or more, each element
of remainder must match qp_n+1
| #(qp_1 ... qp_n) vector of n elements
| #(qp_1 ... qp_n qp_n+1 ooo) vector of n or more, each element
of remainder must match qp_n+1
| #&qp box
| ,pat a pattern
| ,@pat a pattern
| Example: match expression | |
|---|---|
match-let syntax¶
Matches each pattern to the given expression and evaluates with the bond variables. Raises an error if any of the expression fail to match.
| Example: match expression | |
|---|---|
match-let* syntax¶
let* version of match-let.
match-letrec syntax¶
letrec version of match-let